﻿Imports System.IO

''' <summary>
''' This class contains directory helper method(s).
''' </summary>
Public Class uIO

    ''' <summary>
    ''' This method starts at the specified directory, and traverses all subdirectories.
    ''' It returns a List of those directories.
    ''' </summary>
    Public Shared Function GetFilesRecursive(ByVal initial As String, Optional ByVal Pattern As String = "*.*") As List(Of String)
        ' This list stores the results.
        Dim result As New List(Of String), count As Long = 0

        ' This stack stores the directories to process.
        Dim stack As New Stack(Of String)

        ' Add the initial directory
        stack.Push(initial)

        ' Continue processing for each stacked directory
        Do While (stack.Count > 0)
            ' Get top directory string
            Dim dir As String = stack.Pop
            Try
                ' Add all immediate file paths
                result.AddRange(Directory.GetFiles(dir, Pattern))

                ' Loop through all subdirectories and add them to the stack.
                Dim directoryName As String
                For Each directoryName In Directory.GetDirectories(dir)
                    stack.Push(directoryName)
                    count = count + 1
                Next

            Catch ex As Exception
            End Try
            Main.lblStatus.Text = "Caching..." & count
            Application.DoEvents()

        Loop

        ' Return the list
        Return result
    End Function
    Public Shared Function GetFilesRecursiveFTP(ByVal initial As String, ByRef FTP As Utilities.FTP.FTPclient, Optional ByVal Pattern As String = "*.*") As List(Of String)
        ' This list stores the results.
        Dim result As New List(Of String), count As Long = 0, i As Long = 0
        Dim tmp As Utilities.FTP.FTPdirectory
        ' This stack stores the directories to process.
        Dim stack As New Stack(Of String)

        ' Add the initial directory
        stack.Push(initial)

        ' Continue processing for each stacked directory
        Do While (stack.Count > 0)
            ' Get top directory string
            Dim dir As String = stack.Pop
            Try
                ' Add all immediate file paths
                ' result.AddRange(FTP.ListDirectoryDetail("/"))

                tmp = Nothing
                tmp = New Utilities.FTP.FTPdirectory
                tmp = FTP.ListDirectoryDetail(dir)

                For i = 0 To tmp.GetFiles.Count - 1
                    result.Add(tmp.GetFiles.Item(i).FullName)
                    Debug.Print(result.Item(i))
                    count = count + 1
                Next

                ' Loop through all subdirectories and add them to the stack.
   
                For i = 0 To tmp.GetDirectories.Count - 1
                    stack.Push(tmp.GetDirectories.Item(i).FullName)
                    Debug.Print(tmp.GetDirectories(i).FullName)
                Next


                'For Each directoryName In FTP.ListDirectory(dir)

                'stack.Push(directoryName)
                '
                ' Next

            Catch ex As Exception
            End Try
            frmChild.Notify_Status("Caching..." & count)
            Application.DoEvents()

        Loop

        ' Return the list
        Return result
    End Function
End Class
